package ui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class MarqueePanel extends JPanel implements ActionListener {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1312430615716683052L;
	
	private static final int RATE = 12;
    private final Timer timer = new Timer(1000 / RATE, this);
    private final JLabel label = new JLabel();
    private String s;
    private final int n;
    private int index;

    public MarqueePanel(String s, int n) {
        if (s == null || n < 1) {
            throw new IllegalArgumentException("Null string or n < 1");
        }
        StringBuilder sb = new StringBuilder(n);
        for (int i = 0; i < n; i++) {
            sb.append(' ');
        }
        this.s = sb + s + sb;
        this.n = n;
        label.setFont(new Font("Arial", Font.PLAIN, 12));
        label.setText(sb.toString());
        this.add(label);
    }

    public void start() {
        timer.start();
    }

    public void stop() {
        timer.stop();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    	if(s.length() >= 1)
    	{
    		s = s.substring(1);
    		s = s.concat(" ");
    	}
        label.setText(s);
        label.setSize(this.getParent().WIDTH, HEIGHT);
    }
    
    public void appendText(String s)
    {
    	System.out.println("Concat " + s);

        StringBuilder sb = new StringBuilder(n);
        for (int i = 0; i < n; i++) {
            sb.append(' ');
        }
    	
    	this.s = this.s.trim();
    	this.s = this.s.concat(" +++ ");
    	this.s = this.s.concat(s);
    	this.s = this.s + sb;
    	
    }
}